﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.events.*;
    import flash.display.*;
    import org.aswing.geom.*;
    import flash.text.*;
    import flash.ui.*;

    public class JTextComponent extends Component implements EditableComponent {

        private var textField:TextField;
        private var editable:Boolean;
        protected var columnWidth:int;
        protected var rowHeight:int;
        protected var widthMargin:int;
        protected var heightMargin:int;
        protected var columnRowCounted:Boolean;

        public function JTextComponent(){
            this.textField = new TextField();
            this.textField.type = TextFieldType.INPUT;
            this.textField.autoSize = TextFieldAutoSize.NONE;
            this.textField.background = false;
            this.editable = true;
            this.columnRowCounted = false;
            addChild(this.textField);
            this.textField.addEventListener(TextEvent.TEXT_INPUT, this.__onTextComponentTextInput);
        }
        private function __onTextComponentTextInput(_arg1:TextEvent):void{
            var _local2:String;
            var _local3:KeyboardManager;
            if (!this.getTextField().multiline){
                _local2 = _arg1.text;
                _local3 = getKeyboardManager();
                if (_local3){
                    if (((_local3.isKeyDown(Keyboard.CONTROL)) && (_local3.isKeyDown(Keyboard.ENTER)))){
                        if ((((_local2.length == 1)) && ((_local2.charCodeAt(0) == 10)))){
                            _arg1.preventDefault();
                        };
                    };
                };
            };
        }
        public function getTextField():TextField{
            return (this.textField);
        }
        protected function isAutoSize():Boolean{
            return (false);
        }
        override public function setEnabled(_arg1:Boolean):void{
            super.setEnabled(_arg1);
            this.getTextField().selectable = _arg1;
            this.getTextField().mouseEnabled = _arg1;
        }
        public function setEditable(_arg1:Boolean):void{
            if (_arg1 != this.editable){
                this.editable = _arg1;
                if (_arg1){
                    this.getTextField().type = TextFieldType.INPUT;
                } else {
                    this.getTextField().type = TextFieldType.DYNAMIC;
                };
                invalidate();
                this.invalidateColumnRowSize();
                repaint();
            };
        }
        public function isEditable():Boolean{
            return (this.editable);
        }
        override public function setFont(_arg1:ASFont):void{
            super.setFont(_arg1);
            setFontValidated(true);
            if (getFont() != null){
                getFont().apply(this.getTextField());
                this.invalidateColumnRowSize();
            };
        }
        override public function setForeground(_arg1:ASColor):void{
            super.setForeground(_arg1);
            if (getForeground() != null){
                this.getTextField().textColor = getForeground().getRGB();
                this.getTextField().alpha = getForeground().getAlpha();
            };
        }
        public function setText(_arg1:String):void{
            if (this.getTextField().text != _arg1){
                this.getTextField().text = _arg1;
                if (this.isAutoSize()){
                    revalidate();
                };
            };
        }
        public function getText():String{
            return (this.getTextField().text);
        }
        public function setHtmlText(_arg1:String):void{
            this.getTextField().htmlText = _arg1;
            if (this.isAutoSize()){
                revalidate();
            };
        }
        public function getHtmlText():String{
            return (this.getTextField().htmlText);
        }
        public function appendText(_arg1:String):void{
            this.getTextField().appendText(_arg1);
            if (this.isAutoSize()){
                revalidate();
            };
        }
        public function appendByReplace(_arg1:String):void{
            var _local2:int = this.getLength();
            this.getTextField().replaceText(_local2, _local2, _arg1);
        }
        public function replaceSelectedText(_arg1:String):void{
            this.getTextField().replaceSelectedText(_arg1);
        }
        public function replaceText(_arg1:int, _arg2:int, _arg3:String):void{
            this.getTextField().replaceText(_arg1, _arg2, _arg3);
        }
        public function setSelection(_arg1:int, _arg2:int):void{
            this.getTextField().setSelection(_arg1, _arg2);
        }
        public function selectAll():void{
            this.getTextField().setSelection(0, this.getTextField().length);
        }
        public function setCondenseWhite(_arg1:Boolean):void{
            if (this.getTextField().condenseWhite != _arg1){
                this.getTextField().condenseWhite = _arg1;
                revalidate();
            };
        }
        public function isCondenseWhite():Boolean{
            return (this.getTextField().condenseWhite);
        }
        public function setDefaultTextFormat(_arg1:TextFormat):void{
            this.getTextField().defaultTextFormat = _arg1;
        }
        public function getDefaultTextFormat():TextFormat{
            return (this.getTextField().defaultTextFormat);
        }
        public function setTextFormat(_arg1:TextFormat, _arg2:int=-1, _arg3:int=-1):void{
            this.getTextField().setTextFormat(_arg1, _arg2, _arg3);
        }
        public function getTextFormat(_arg1:int=-1, _arg2:int=-1):TextFormat{
            return (this.getTextField().getTextFormat(_arg1, _arg2));
        }
        public function setDisplayAsPassword(_arg1:Boolean):void{
            this.getTextField().displayAsPassword = _arg1;
        }
        public function isDisplayAsPassword():Boolean{
            return (this.getTextField().displayAsPassword);
        }
        public function getLength():int{
            return (this.getTextField().length);
        }
        public function setMaxChars(_arg1:int):void{
            this.getTextField().maxChars = _arg1;
        }
        public function getMaxChars():int{
            return (this.getTextField().maxChars);
        }
        public function setRestrict(_arg1:String):void{
            this.getTextField().restrict = _arg1;
        }
        public function getRestrict():String{
            return (this.getTextField().restrict);
        }
        public function getSelectionBeginIndex():int{
            return (this.getTextField().selectionBeginIndex);
        }
        public function getSelectionEndIndex():int{
            return (this.getTextField().selectionEndIndex);
        }
        public function setCSS(_arg1:StyleSheet):void{
            this.getTextField().styleSheet = _arg1;
            if (this.isAutoSize()){
                revalidate();
            };
        }
        public function getCSS():StyleSheet{
            return (this.getTextField().styleSheet);
        }
        public function setWordWrap(_arg1:Boolean):void{
            this.getTextField().wordWrap = _arg1;
            if (this.isAutoSize()){
                revalidate();
            };
        }
        public function isWordWrap():Boolean{
            return (this.getTextField().wordWrap);
        }
        public function setUseRichTextClipboard(_arg1:Boolean):void{
            this.getTextField().useRichTextClipboard = _arg1;
        }
        public function isUseRichTextClipboard():Boolean{
            return (this.getTextField().useRichTextClipboard);
        }
        override protected function countPreferredSize():IntDimension{
            throw (new Error("Subclass of JTextComponent need implement this method : countPreferredSize!"));
        }
        protected function invalidateColumnRowSize():void{
            this.columnRowCounted = false;
        }
        protected function getColumnWidth():int{
            if (!this.columnRowCounted){
                this.countColumnRowSize();
            };
            return (this.columnWidth);
        }
        protected function getRowHeight():int{
            if (!this.columnRowCounted){
                this.countColumnRowSize();
            };
            return (this.rowHeight);
        }
        protected function getWidthMargin():int{
            if (!this.columnRowCounted){
                this.countColumnRowSize();
            };
            return (this.widthMargin);
        }
        protected function getHeightMargin():int{
            if (!this.columnRowCounted){
                this.countColumnRowSize();
            };
            return (this.heightMargin);
        }
        protected function getTextFieldAutoSizedSize(_arg1:int=0, _arg2:int=0):IntDimension{
            var _local3:TextField = this.getTextField();
            var _local4:IntDimension = new IntDimension(_local3.width, _local3.height);
            var _local5:String = _local3.autoSize;
            if (_arg1 != 0){
                _local3.width = _arg1;
            };
            if (_arg2 != 0){
                _local3.height = _arg2;
            };
            _local3.autoSize = TextFieldAutoSize.LEFT;
            var _local6:IntDimension = new IntDimension(_local3.width, _local3.height);
            _local3.autoSize = _local5;
            _local3.width = _local4.width;
            _local3.height = _local4.height;
            if (_arg1 != 0){
                _local6.width = _arg1;
            };
            if (_arg2 != 0){
                _local6.height = _arg2;
            };
            return (_local6);
        }
        protected function countColumnRowSize():void{
            var _local1 = "mmmmm";
            var _local2:TextFormat = getFont().getTextFormat();
            var _local3:IntDimension = AsWingUtils.computeStringSize(_local2, _local1, true, this.getTextField());
            var _local4:IntDimension = AsWingUtils.computeStringSize(_local2, _local1, false, this.getTextField());
            if (_local2.font == "NSimSun"){
                this.columnWidth = Math.round(((_local4.width / 4) + (int(_local2.size) / 6)));
            } else {
                this.columnWidth = (_local4.width / 5);
            };
            this.rowHeight = _local4.height;
            this.widthMargin = (_local3.width - _local4.width);
            this.heightMargin = (_local3.height - _local4.height);
            this.columnRowCounted = true;
        }
        override public function getInternalFocusObject():InteractiveObject{
            return (this.getTextField());
        }
        override protected function paint(_arg1:IntRectangle):void{
            super.paint(_arg1);
            this.applyBoundsToText(_arg1);
        }
        protected function applyBoundsToText(_arg1:IntRectangle):void{
            var _local2:TextField = this.getTextField();
            _local2.x = _arg1.x;
            _local2.y = _arg1.y;
            _local2.width = _arg1.width;
            _local2.height = _arg1.height;
        }

    }
}//package org.aswing 
